package com.ikkong.shine.controller;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.shine.model.Doctor;
import com.ikkong.shine.model.QueueType;
import com.ikkong.shine.model.Rlt_doctor2queue_type;
import com.ikkong.shine.service.DoctorService;
import com.ikkong.shine.service.QueueTypeService;
import com.ikkong.shine.service.Rlt_doctor2queue_typeService;
import com.ikkong.shine.service.impl.DoctorServiceImpl;
import com.ikkong.shine.service.impl.QueueTypeServiceImpl;
import com.ikkong.shine.service.impl.Rlt_doctor2queue_typeServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.kit.StrKit;

/**
 * Generated by Blade.
 * 2017-03-24 14:38:29
 */
public class Rlt_doctor2queue_typeController extends UrlPermissController {
	private static String CODE = "rlt_doctor2queue_type";
	private static String PERFIX = "rlt_doctor2queue_type";
	private static String LIST_SOURCE = "Rlt_doctor2queue_type.list";
	private static String BASE_PATH = "/platform/rlt_doctor2queue_type/";
	
	Rlt_doctor2queue_typeService service = new Rlt_doctor2queue_typeServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "rlt_doctor2queue_type.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "rlt_doctor2queue_type_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Rlt_doctor2queue_type rlt_doctor2queue_type = service.findById(id);
		setAttr("model", JsonKit.toJson(rlt_doctor2queue_type));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "rlt_doctor2queue_type_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Rlt_doctor2queue_type rlt_doctor2queue_type = service.findById(id);
		setAttr("model", JsonKit.toJson(rlt_doctor2queue_type));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "rlt_doctor2queue_type_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Rlt_doctor2queue_type rlt_doctor2queue_type = mapping(PERFIX, Rlt_doctor2queue_type.class);
		boolean temp = service.save(rlt_doctor2queue_type);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Rlt_doctor2queue_type rlt_doctor2queue_type = mapping(PERFIX, Rlt_doctor2queue_type.class);
		//rlt_doctor2queue_type.setVersion(rlt_doctor2queue_type.getVersion()+1);
		boolean temp = service.update(rlt_doctor2queue_type);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	/*医生编号	外键	必要	login_id	Varchar(100)	NOT NULL	对应doctor医生视图中login_id字段
	队列类型编号	外键	必要	queue_type_source_id	Varchar(100)	NOT NULL	对应Queue_Type排队队列视图中source_id字段
	排班标识		必要	onduty	Varchar(7)	NOT NULL	可选列
	7位为每周上下午的标识，
	0不出诊
	1全天
	2上午
	3下午
	例如：0131111
	表示周一不出诊
	周三下午出诊
	其他时间全天*/
	public void save_rostering(){
		Map<String, Object> map=new HashMap<String, Object>();
		String schedulings_strs= getPara("schedulings");
		if(StrKit.isBlank(schedulings_strs)){
			map.put("code", -1);
			map.put("msg", "请传值");
			renderJson(map);
			return;
		}else{
			JSONArray schedulingsList= JSONArray.parseArray(schedulings_strs);
			if(schedulingsList!=null){
				Rlt_doctor2queue_type rdq= null;
				for (Object json : schedulingsList) {
					JSONObject schedulings_json = (JSONObject) json;
					rdq=new Rlt_doctor2queue_type();
					DoctorService dsserver=new DoctorServiceImpl();
					Doctor doctor= dsserver.findByLogin_id(schedulings_json.getString("login_id"));
					int doctor_id=doctor.getDoctor_id();
					
					QueueTypeService qtservice=new QueueTypeServiceImpl();
					QueueType queuetype= qtservice.findBySource_id(schedulings_json.getString("queue_type_source_id"));
					int queuetypeid=queuetype.getQueue_type_id();
					
					int i= service.findInt(doctor_id,queuetypeid);
					if(i>0){
						String sql="DELETE FROM rlt_doctor2queue_type WHERE doctor_id="+doctor_id+" and queue_type_id="+queuetypeid+"";
						Db.init().delete(sql, null);
						rdq.setDoctor_id(doctor_id);
						rdq.setQueue_type_id(queuetypeid);
						rdq.setOnduty(schedulings_json.getString("onduty"));
						boolean bool=false;
						try {
							bool= service.save(rdq);
							if(bool){
								map.put("code", 200);
								map.put("msg", "成功");
								renderJson(map);
							}else{
								map.put("code", -1);
								map.put("msg", "失败");
								renderJson(map);
								return;
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
						
					}else{
						rdq.setDoctor_id(doctor_id);
						rdq.setQueue_type_id(queuetypeid);
						rdq.setOnduty(schedulings_json.getString("onduty"));
						boolean bool= false;
						try {
							bool=service.save(rdq);
							if(bool){
								map.put("code", 200);
								map.put("msg", "保存成功");
								renderJson(map);
							}else{
								map.put("code", -1);
								map.put("msg", "保存失败");
								renderJson(map);
								return;
							}
						} catch (Exception e) {
							map.put("code", -1);
							map.put("msg", "保存失败,传入的医生工号:"+schedulings_json.getString("login_id")+"或者队列工号:"+schedulings_json.getString("queue_type_source_id")+"已存在");
							renderJson(map);
							return;
						}
								
						
					}
				}
			}
		}
		/*String login_id=getPara("login_id");
		if(ToolString.isBlank(login_id)){
			map.put("code", -1);
			map.put("msg", "请传入医生编号");
			renderJson(map);
			return;
		}
		DoctorService dsserver=new DoctorServiceImpl();
		Doctor doctor= dsserver.findByLogin_id(login_id);
		if(null==doctor){
			map.put("code", -1);
			map.put("msg", "请传入正确的医生编号");
			renderJson(map);
			return;
		}
		int doctor_id=doctor.getDoctor_id();
		String queue_type_source_id=getPara("queue_type_source_id");
		if(ToolString.isBlank(queue_type_source_id)){
			map.put("code", -1);
			map.put("msg", "请传入队列类型编号");
			renderJson(map);
			return;
		}
		QueueTypeService qtservice=new QueueTypeServiceImpl();
		QueueType queuetype= qtservice.findBySource_id(queue_type_source_id);
		if(null==queuetype){
			map.put("code", -1);
			map.put("msg", "请传入正确的队列类型编号");
			renderJson(map);
			return;
		}
		int queuetypeid=queuetype.getQueue_type_id();
		String onduty=getPara("onduty");
		if(ToolString.isBlank(onduty)){
			map.put("code", -1);
			map.put("msg", "请传入排班标识");
			renderJson(map);
			return;
		}
		int i= service.findInt(doctor_id, queuetypeid);
		if(i>0){
			String sql="DELETE FROM rlt_doctor2queue_type WHERE doctor_id="+doctor_id+" and queue_type_id="+queuetypeid+"";
			Db.init().delete(sql, null);
			Rlt_doctor2queue_type rdq=new Rlt_doctor2queue_type();
			rdq.setDoctor_id(doctor_id);
			rdq.setQueue_type_id(queuetypeid);
			rdq.setOnduty(onduty);
			boolean bool= service.save(rdq);
			if(bool){
				map.put("code", 200);
				map.put("msg", "保存成功");
				renderJson(map);
			}else{
				map.put("code", -1);
				map.put("msg", "保存失败");
				renderJson(map);
				return;
			}
		}else{
			Rlt_doctor2queue_type rdq=new Rlt_doctor2queue_type();
			rdq.setDoctor_id(doctor_id);
			rdq.setQueue_type_id(queuetypeid);
			rdq.setOnduty(onduty);
			boolean bool= false;
			try {
				bool=service.save(rdq);
				if(bool){
					map.put("code", 200);
					map.put("msg", "保存成功");
					renderJson(map);
				}else{
					map.put("code", -1);
					map.put("msg", "保存失败");
					renderJson(map);
					return;
				}
			} catch (Exception e) {
				map.put("code", -1);
				map.put("msg", "保存失败,传入的医生工号:"+login_id+"或者队列工号:"+queue_type_source_id+"已存在");
				renderJson(map);
				return;
			}
					
			
		}*/
		renderJson(map);
	}
}
